<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.9.1"/>
<title>cocoOS: C:/Users/peter_000/Desktop/cocoOS/common/src/docSemaphores.h File Reference</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { init_search(); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td style="padding-left: 0.5em;">
   <div id="projectname">cocoOS
   &#160;<span id="projectnumber">5.0.3</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.9.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="annotated.html"><span>Data&#160;Structures</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>Globals</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('doc_semaphores_8h.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">docSemaphores.h File Reference</div>  </div>
</div><!--header-->
<div class="contents">

<p><a href="doc_semaphores_8h_source.html">Go to the source code of this file.</a></p>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p>cocoOS supports binary and counting semaphores. A semaphore is created in the main function using one of the create functions: <a class="el" href="os__sem_8c.html#aaecd95706696b8ff18d5ef74d139d750">sem_bin_create()</a> or <a class="el" href="os__sem_8c.html#a9cfb5b3cc72fdaed17009a0cae7fd725">sem_counting_create()</a>. </p><div class="fragment"><div class="line">Sem_t bin_sem;</div>
<div class="line">Sem_t count_sem;</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">int</span> main( <span class="keywordtype">void</span> ) {</div>
<div class="line">    bin_sem = <a class="code" href="os__sem_8c.html#aaecd95706696b8ff18d5ef74d139d750">sem_bin_create</a>( initValue );</div>
<div class="line">    count_sem = <a class="code" href="os__sem_8c.html#a9cfb5b3cc72fdaed17009a0cae7fd725">sem_counting_create</a>( maxCount, initValue );</div>
<div class="line">    ...</div>
<div class="line">}</div>
</div><!-- fragment --><p> <em>initValue</em> is the initial value for the semaphore counter. For a binary semaphore this can be 0 or 1, and for the counting semaphore 0-255. <br />
 <em>maxCount</em> is the maximum value the counting semaphore can reach. <br />
 A call to sem_wait() decrements the semaphore value by one if it is not 0. If it is 0, the value is not decremented, but the task is put into the waiting state. <br />
 A call to sem_signal() increments the semaphore value by one if it is not 1 in case of a binary semaphore, or maxCount in case of a counting semaphore. The semaphore is only incremented if no tasks are waiting for the semaphore. If there are a waiting task, the semaphore value is left unchanged and the highest priority waiting task gets the semaphore and is put back to ready state again.</p>
<p>Interrupt service routines should not call sem_signal(), because task switching is not allowed during interrupts. Instead an ISR can call sem_ISR_signal() that will release the semaphore, but any task switch will be done when both the ISR and the interrupted task has returned.</p>
<p><br />
 <br />
<b> Task Synchronization Example </b> <br />
 <br />
In the example below, sem is a semaphore initialized with value 0. Task1 has higher priority than Task2. Task 1 will execute down to the sem_wait( sem ) call, where it will block because the semaphore value is 0. Then Task2 is scheduled to execute, and will run down to the line where the semaphore is signaled. This will cause the Task2 to block, and Task1 is scheduled to execute due to its higher priority. </p><div class="fragment"><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> task1( <span class="keywordtype">void</span> ) {</div>
<div class="line">  task_open();  </div>
<div class="line">  <span class="keywordflow">for</span> (;;) {</div>
<div class="line">    ...</div>
<div class="line">    sem_wait( sem );</div>
<div class="line">    ...</div>
<div class="line">  }</div>
<div class="line">  task_close();</div>
<div class="line">} </div>
<div class="line"></div>
<div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> task2( <span class="keywordtype">void</span> ) {</div>
<div class="line">  task_open();  </div>
<div class="line">  <span class="keywordflow">for</span> (;;) {</div>
<div class="line">    ...</div>
<div class="line">    sem_signal( sem );</div>
<div class="line">    ...</div>
<div class="line">  }</div>
<div class="line">  task_close();</div>
<div class="line">}</div>
</div><!-- fragment --><p><b>Semaphore API</b> The Semaphore API consists of the following functions and macros:</p><ul>
<li><a class="el" href="os__sem_8c.html#aaecd95706696b8ff18d5ef74d139d750">sem_bin_create()</a></li>
<li><a class="el" href="os__sem_8c.html#a9cfb5b3cc72fdaed17009a0cae7fd725">sem_counting_create()</a></li>
<li>sem_wait()</li>
<li>sem_signal()</li>
<li>sem_ISR_signal() </li>
</ul>
</div></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="dir_bdd9a5d540de89e9fe90efdfc6973a4f.html">common</a></li><li class="navelem"><a class="el" href="dir_4e4d22e38ca4e2e8207825ee2d00bc54.html">src</a></li><li class="navelem"><a class="el" href="doc_semaphores_8h.html">docSemaphores.h</a></li>
    <li class="footer">Generated on Tue Aug 28 2018 17:58:05 for cocoOS by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.9.1 </li>
  </ul>
</div>
</body>
</html>
